/**
 * 格式化时间 parseTime(new Date(),'{y}-{m}-{d} {h}:{i}')
 * @param {Date} time 
 * @param {String} pattern 
 * @returns 
 */
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }

  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str;
}

/**
 * 获取当前时间
 * @returns String
 */
export function getNoon() {
  const date = new Date()
  const h = date.getHours()

  let n = ''
  if (h > 5 && h < 9) {
    n = '早上'
  } else if (h > 8 && h < 12) {
    n = '上午'
  } else if (h > 11 && h < 13) {
    n = '中午'
  } else if (h > 12 && h < 18) {
    n = '下午'
  } else {
    n = '晚上'
  }
  // console.log('--hour--', h, n);
  return n + '好'
}

/**
 * 根据program获取当前程序的类型
 * @param {Number} program 
 * @returns 
 */
export function getProgram(program) {
  const programs = ['自助打印', '加急打印'];
  if (typeof program != 'number')
    program = parseInt(program);
  if (program >= programs.length)
    return '测试程序'
  const text = programs[program];
  return text;
}

/**
 * 获取文件处理状态
 * @param {Number} status 
 * @returns 
 */
export function getStatus(status) {
  const statusArray = ['订单已提交', '商家已经接收文件', '打印完成', '配送完成'];
  if (typeof status != 'number')
    status = parseInt(status);
  if (status >= statusArray.length)
    return '文件正在处理中';
  const text = statusArray[status];
  return text;
}